package com.stefanini.worldcupinformation.controladores;

import com.stefanini.worldcupinformation.ejbs.UserManager;
import com.stefanini.worldcupinformation.filters.SecurityFilter;
import com.stefanini.worldcupinformationpersistence.entities.Usuario;
import java.io.IOException;
import java.io.Serializable;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

@Named("loginController")
@RequestScoped
public class LoginController implements Serializable {

    private static final long serialVersionUID = 1L;

    private String nameUser;
    private String password;

    @EJB
    private UserManager userManager;

    public void login() {
        Usuario user = userManager.verificarLogin(this.nameUser, this.password);

        FacesContext ctx = FacesContext.getCurrentInstance();

        if (user == null) {
            ctx.addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Usuario o contrseña invalido"));
        } else {
            HttpSession session = (HttpSession) ctx.getExternalContext().getSession(true);//True para crear una nueva session y desechar las que existan
            session.setAttribute(SecurityFilter.LOGGED_USER, user.getNombre());
            session.setAttribute(SecurityFilter.IS_LOGGED, Boolean.TRUE);
            try {
                ctx.getExternalContext().redirect(
                        "/worldCupInformation/pages/administrator/mainAdministrator.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void changeLanguage() {
        if (FacesContext.getCurrentInstance().getViewRoot().getLocale().equals(new Locale("es"))) {
            FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.ENGLISH);
        } else {
            FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale("es"));
        }
    }

    public void logout() {
        try {
            FacesContext ctx = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) ctx.getExternalContext().getSession(false);
            session.invalidate();
            ctx.getExternalContext().redirect("/worldCupInformation/login.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, "Error al redireccionar", ex);
        }
    }

    public String getNameUser() {
        return nameUser;
    }

    public void setNameUser(String nameUser) {
        this.nameUser = nameUser;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
